//
//  locallViewController.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/15.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "locallViewController.h"
#import "localtableview.h"
#import "firstTableviewcell.h"
#import "secondTableViewCell.h"
#import "APPTools.h"
#import "ModelOfFirst.h"
#import "ModelOfSecond.h"
#import <UIImageView+WebCache.h>
#import "FirstDetailViewController.h"
#import "SecondDetailViewController.h"
#import "LocalLunBo.h"
#import "LunBoDetailViewController.h"
#import <DKNightVersion.h>
#import <MJRefresh.h>
//#import  <MJRefreshNormalHeader.h>
#import <MJRefreshAutoNormalFooter.h>
@interface locallViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)localtableview *table;
@property (nonatomic,retain)NSMutableArray *arrSort;//分类
@property (nonatomic,retain)NSMutableArray *arrFood;//美食
@property (nonatomic,retain)NSMutableDictionary *mdic;
@property (nonatomic,retain)LocalLunBo *lunboView;
@property (nonatomic, retain) NSMutableArray *arrLunbo; //轮播
@property (nonatomic, retain) NSMutableArray *arrLunboSelect; // 轮播点击
@end

@implementation locallViewController

#pragma mark - ------------ 内存管理 ---------------

-(void)dealloc{
    
    [_table release];
    [_arrFood release];
    [_arrSort release];
    [_mdic release];
    [_lunboView release];
    [_arrLunboSelect release];
    [_arrLunbo release];
    [super dealloc];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSerVer];
    [self DataHandler];
    [self createTableview];
    [self refresh];
    self.navigationItem.title = @"本地";
    // Do any additional setup after loading the view.
}

#pragma mark - ------------ 解析数据 ---------------

-(void)DataHandler{
    [APPTools GETWithURL:@"https://open.seriousapps.cn/4/tab/list.json?city_id=1&lat=38.88341375456474&lng=121.5446097782637" par:nil success:^(id responseObject) {
        
        //轮播图
        self.arrLunbo = [NSMutableArray array];
        self.arrLunboSelect = [NSMutableArray array];
        NSArray *arrLunBo = responseObject[@"content"][0][@"data"][@"banner_list"];
        for (NSDictionary *dic in arrLunBo) {
            [_arrLunbo addObject:dic[@"img_url"]];
            [_arrLunboSelect addObject:dic[@"enjoy_url"]];
        }
        [self createLunbo];
        
        
        //第一块解析数据
        NSArray *arrSORT = responseObject[@"content"][1][@"data"][@"group_list"];
        self.arrSort = [ModelOfFirst modelHandleWithArrar:arrSORT];
        
        [self.table reloadData];
            } filed:^(NSError *error) {
}];
    
   [APPTools GETWithURL:@"https://open.seriousapps.cn/4/tab/local_product_list.json?city_id=140&lat=38.88343257969636&lng=121.5447415929212&page=1" par:nil success:^(id responseObject) {
       self.arrFood = [NSMutableArray array];
       //第二块解析数据
       NSArray *arrNew = [[NSArray arrayWithObject:responseObject] firstObject];
       
       for (NSDictionary *dic in arrNew) {
           
           NSDictionary *data = dic[@"data"];
           
           ModelOfSecond *model = [ModelOfSecond modelWithDictionary:data];
           
           [self.arrFood addObject:model];
           
       }
      
       [self.table reloadData];
       
       
   } filed:^(NSError *error) {
       
   }];
    
    
    
}
#pragma mark - ------------ 创建轮播图 并点击跳转  ---------------

- (void)createLunbo
{
    
    if (_arrLunbo.count != 0) {
        
        self.lunboView = [[LocalLunBo alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width , 200) imgArr:_arrLunbo selectImageBlock:^(NSInteger index) {
            NSLog(@"点击了轮播图第%ld张", index);
            
           LunBoDetailViewController *lunbo = [[LunBoDetailViewController alloc]init];
            lunbo.url = _arrLunboSelect[index];
            
            [self.navigationController pushViewController:lunbo animated:YES] ;
            
        }];
        
        self.table.tableHeaderView = _lunboView;
        self.table.tableHeaderView.userInteractionEnabled = YES;
        [_lunboView release];
        
    }
}
#pragma mark - ------------ 加载 刷新 ---------------

-(void)refresh{
   
    //刷新
   self.table.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       [self DataHandler];
       [_table.mj_header endRefreshing];
   }];
   
    //加载
    
    self.table.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self DataHandler];
        [_table.mj_footer endRefreshing];
    }];
 

}




#pragma mark - ------------ 创建tableview ---------------

-(void)createTableview{
    
    self.table = [[localtableview alloc]initWithFrame:[UIScreen mainScreen].bounds style:0];
    [self.view addSubview:self.table];
    self.table.delegate   =  self;
    self.table.dataSource = self;
    [self.table registerClass:[firstTableviewcell class] forCellReuseIdentifier:@"pool"];
       
    [self.table registerClass:[secondTableViewCell class] forCellReuseIdentifier:@"pool1"];
    //创建第二模块的头部
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrFood.count;
}
#pragma mark - ------------  赋值 ---------------

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{
            firstTableviewcell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
            cell.arrJieShou = _arrSort;//将数据的数组传入
            cell.dk_backgroundColorPicker = DKColorPickerWithKey(BAR);
            return cell;
        }
            break;
            
        default:{
        
            secondTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
            
            
            ModelOfSecond *model = _arrFood[indexPath.row];
            cell1.labelname.text = model.name;
            [cell1.imageview1 sd_setImageWithURL:[NSURL URLWithString:model.product_image]];
            cell1.labelcontent.text = model.short_description;
            
            //现价
            
            NSInteger i = model.price.integerValue/100;
                NSString *str = [NSString stringWithFormat:@"%ld",(long)i];
            cell1.labelpriceing.text = [NSString stringWithFormat:@"%@%@%@",str,@"元/",model.entity_name];
            cell1.labelpriceing.tintColor = [UIColor redColor];
            cell1.labelpriceing.font = [UIFont systemFontOfSize:16];

            //原价
            NSString *str1 = [NSString stringWithFormat:@"%@%@%@",[model.original_price.stringValue substringToIndex:3],@"元/",model.entity_name];
                NSMutableAttributedString *attri = [[NSMutableAttributedString alloc]initWithString:str1];
            
                [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleDouble | NSUnderlinePatternSolid) range:NSMakeRange(0, [str1 length])];
            
                cell1.labelprice.attributedText = attri;
                cell1.labelprice.textColor = [UIColor lightGrayColor];

            return cell1;
        }
            
            break;
    }
  
}

#pragma mark - ------------ 返回高度 ---------------

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 310;
    }else{
        return 300;
    }

}

#pragma mark - ------------ 通知中心分类点击 ---------------
- (void)addSerVer
{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sortDetail:) name:@"index" object:nil];
}

- (void)sortDetail:(NSNotification *)noti
{
    
    NSLog(@"点击第%@个分类item", noti.object);
    
    NSInteger num = [noti.object integerValue];
    
    
  FirstDetailViewController *detail = [[FirstDetailViewController alloc]init];
    
    //赋值部分
    
    ModelOfFirst *m = _arrSort[num];
   
    detail.strID = [m.enjoy_url substringWithRange:NSMakeRange(33, 6)];//ID
    detail.NaviTitle = m.title;
   [self.navigationController pushViewController:detail animated:YES];
   
    [detail release];
    
}
#pragma mark - ------------ 第2模块跳转页面 ---------------

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row != 0 ) {
        SecondDetailViewController *vc2 = [[SecondDetailViewController alloc]init];
        ModelOfSecond *model = _arrFood[indexPath.row];
        vc2.DetailID = model.product_id;
        
        
        [self.navigationController  pushViewController:vc2 animated:YES];
    }


}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
